package com.linjiu.easyphotos.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.linjiu.easyphotos.R;

/**
 * 项目名称：com.linjiu.easyphotos.ui.dialog
 * 创建人:LuoFG
 * 创建时间:2024/5/31  14:37
 * 描述:
 * 作用:
 */
public class DialogSave implements View.OnClickListener {
    TextView tvTitle;
    Button btnSave;
    Button btnCancel;
    private Context mContext;
    private Dialog dialog;
    private IBack backAgree;
    private IBack backCancel;
    private View contentView;


    public interface IBack{
        void back();
    }


    public DialogSave setAgreeBack(IBack back) {
        this.backAgree = back;
        return this;
    }
    public DialogSave setCancel(IBack backCancels) {
        this.backCancel = backCancels;
        return this;
    }

    public DialogSave(Context mContext) {
        this.mContext = mContext;
        initView(mContext);
    }

    /**
     * dialog初始化
     *
     * @param mContext
     */
    private void initView(Context mContext) {
        dialog = new Dialog(mContext, R.style.dialog_style);
        contentView= LayoutInflater.from(mContext).inflate(R.layout.dialog_save, null);
        dialog.setContentView(contentView);
        Window win = dialog.getWindow();
        win.setGravity(Gravity.CENTER);
        win.setWindowAnimations(R.style.DialogAnimationFade);
        win.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);

        tvTitle= (TextView) contentView.findViewById(R.id.tv_title);
        btnCancel= (Button) contentView.findViewById(R.id.btn_cancel);
        btnSave= (Button) contentView.findViewById(R.id.btn_save);
        btnCancel.setOnClickListener(this);
        btnSave.setOnClickListener(this);
    }



    public DialogSave setTitle(String title){
        tvTitle.setText(title);
        return this;
    }


    /**
     * 对话框显示
     */
    public DialogSave show() {
        try {
            if (mContext == null) {
                return this;
            }

            if (dialog == null || dialog.isShowing()) {
                return this;
            }
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }


    /**
     * 对话框关闭
     */
    public DialogSave dismiss() {
        try {
            if (mContext == null) {
                return this;
            }

            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_save) {
            if (backAgree != null) {
                backAgree.back();
            }
            dismiss();
        } else if (id == R.id.btn_cancel) {
            if (backCancel != null) {
                backCancel.back();
            }
            dismiss();
        }
    }



}

